package com.sky.aop;

import com.sky.constant.AutoFillConstant;
import com.sky.utils.IdGenerator;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class AutoIdAop {

    @Pointcut(" execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.anotion.AutoId)")
    public void autoIdPointcut() {}
    @Before("autoIdPointcut()")
    public void autoGenerateId(JoinPoint joinPoint) throws Exception {
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length == 0) return;
        Object entity = args[0];

        try {
            Method setId = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_ID, String.class);
            setId.setAccessible(true);
            String id = IdGenerator.generateId();
            setId.invoke(entity, id);
        } catch (NoSuchMethodException e) {
            // 如果没有setId方法，可以忽略或打印日志
        }
    }
}

